package controllers

import (
	"encoding/json"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"userr/cache"
	"userr/models"
)

type MainControll struct {
	beego.Controller
}
type Res struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

// 注册
func (c *MainControll) Register() {
	username := c.GetString("username")
	password := c.GetString("password")

	user := models.User{
		Username: username,
		Password: password,
	}

	if username == "" || password == "" {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "账号或密码不能为空",
			Data: nil,
		}
		c.ServeJSON()
		return
	}

	err := models.DB.Debug().Where("username=?", username).Limit(1).Find(&user).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "系统报错",
			Data: nil,
		}
		c.ServeJSON()
		return
	}

	if user.ID != 0 {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "用户名已存在,请重新注册",
			Data: nil,
		}
		c.ServeJSON()
		return
	}

	err = models.DB.Debug().Create(&user).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "注册失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "注册成功",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
}

// 添加
func (c *MainControll) Add() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	name := c.GetString("name")
	img := c.GetString("img")
	status := c.GetString("status")
	desc := c.GetString("desc")
	price := c.GetString("price")

	wp := models.Wp{
		Name:   name,
		Img:    img,
		Status: status,
		Desc:   desc,
		Price:  price,
	}

	err := models.DB.Create(&wp).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		//缓存
		key := fmt.Sprintf("user_add_%d", wp.ID)
		jsonData, _ := json.Marshal(&wp)
		cache.Rdb.Set(cache.Ctx, key, jsonData, 0)

		c.Data["json"] = Res{
			Code: 200,
			Msg:  "添加成功",
			Data: wp,
		}
		c.ServeJSON()
		return
	}
}

// 删除
func (c *MainControll) Del() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	id, _ := c.GetInt("id")
	var wp models.Wp
	err := models.DB.Debug().Delete(&wp, id).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "删除失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		//缓存

		c.Data["json"] = Res{
			Code: 200,
			Msg:  "删除成功",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
}

// 列表
func (c *MainControll) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	var wp []models.Wp
	err := models.DB.Find(&wp).Error

	//缓存
	key := fmt.Sprintf("wp_list")
	res, _ := cache.Rdb.Get(cache.Ctx, key).Result()
	if res != "" {
		json.Unmarshal([]byte(res), &wp)
	} else {
		jsonData, _ := json.Marshal(wp)
		cache.Rdb.Set(cache.Ctx, key, jsonData, 0)
	}

	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "查询成功",
			Data: wp,
		}
		c.ServeJSON()
		return
	}
}

// 上传图片
func (c *MainControll) Upload() {
	file, header, err := c.GetFile("img")
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "文件获取失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	defer file.Close()

	if header.Size > 2*1024*1024 {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "文件大小不能超过2MB",
			Data: nil,
		}
		c.ServeJSON()
		return
	}

	if header.Filename != ".JPG" || header.Filename != ".PNG" {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "格式不正错",
			Data: nil,
		}
		c.ServeJSON()
		return
	}

	path := "static/img/" + header.Filename
	err = c.SaveToFile("img", path)
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "上传失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		Url := "http://127.0.0.1:8080/" + path
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "上传成功",
			Data: Url,
		}
		c.ServeJSON()
		return
	}
}
